/**
 * elancom Corporation copy right 2012 - 2015  
 *   
 * $Header: https://192.168.1.224/svn/elancom/product/elancom-platform/trunk/src/main/java/com/elancom/framework/component/HuntScope.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Id: HuntScope.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Author: lanzhouxue $
 * $Date: 2012-07-02 11:09:12 +0800 (Mon, 02 Jul 2012) $
 * $Revision: 1034 $
 */
package com.elancom.framework.component.enums;

/**
 * @author elancom
 * @date Apr 27, 2012
 */
public enum HuntScope {
	RequestParemeter, RequestAttribute, SessionAttribute, ServletContextAttribute/* ComponentContext,SpringContext */;

	private HuntScope() {
		bit = (1 << ordinal());
	}

	private final int bit;

	public final int getBit() {
		return bit;
	}

	// 0000 0010 <- 已有scopes
	// 0000 0001 <- 参数scope
	// 0000 0000 <- 等0说明没用使用该feature
	public static boolean isUse(int scopes, HuntScope... scope) {
		for (final HuntScope s : scope) {
			if ((scopes & s.getBit()) == 0) {
				return false;
			}
		}
		return true;
	}

	// 如果使用则直接或操作,否则先取反再与操作,去掉标志位
	public static int use(int scopes, HuntScope scope, boolean isuse) {
		return isuse ? scopes | scope.getBit() : scopes & ~scope.getBit();
	}

	public static void main(String[] args) {
		final int $2z = Integer.parseInt("01", Character.MIN_RADIX);
		System.out.println($2z + "&" + HuntScope.RequestParemeter.getBit() + //
				" = "//
				+ ($2z & HuntScope.RequestParemeter.getBit()));

		// ***** test ******
		int scopes = use(0, RequestAttribute, true);
		System.out.println("使用了request范围:" + isUse(scopes, RequestAttribute) + "  值：" + scopes);
		System.out.println("并没使用context范围:" + !isUse(scopes, ServletContextAttribute) + "  值：" + scopes);
		scopes = use(scopes, ServletContextAttribute, true);
		System.out.println("使用了request,context范围:" + (isUse(scopes, RequestAttribute) && isUse(scopes, ServletContextAttribute)) + "  值："
				+ scopes);
		scopes = use(scopes, RequestAttribute, false);
		System.out
				.println("取消了request范围:" + (!isUse(scopes, RequestAttribute) && isUse(scopes, ServletContextAttribute)) + "  值：" + scopes);
	}
}
